package com.m.crm.web;

import com.m.crm.dto.BaseResult;
import com.m.crm.dto.BootStrapTableResult;
import com.m.crm.entity.Rank;
import com.m.crm.service.RankService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by yugoh on 2017/5/22.
 */
@RestController
@RequestMapping("/ranks")
public class RankController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private final RankService rankService;

    @Autowired
    public RankController(RankService rankService) {
        this.rankService = rankService;
    }

    /**
     * 获取级别列表
     *
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public BootStrapTableResult<Rank> list(Integer offset, Integer limit) {
        rankService.listRank(offset, limit);
        return null;
    }

    /**
     * 更新级别信息
     *
     * @param rank
     * @return
     */
    @RequestMapping(value = "/{rankId}", method = RequestMethod.PUT, produces = {"application/json;charset=UTF-8"})
    public Rank update(Rank rank) {
        // TODO
        return null;
    }

    /**
     * 添加一个级别
     *
     * @param rank
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public BaseResult<Object> add(Rank rank) {
        log.info("invoke----------/ranks.post");
        Rank resultRank;
        try {
            resultRank = rankService.saveRank(rank);
        } catch (Exception e) {
            return new BaseResult<>(false, e.getMessage());
        }
        return new BaseResult<>(true, resultRank);
    }

    /**
     * 查询一个级别
     *
     * @return
     */
    @RequestMapping(value = "/{rankId}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Rank getRank(@PathVariable Integer rankId) {
        log.info("invoke----------/ranks/" + rankId + ".get");
        return rankService.getRankById(rankId);
    }
}
